package vn.component;

import java.util.ArrayList;

import vn.game.ExtEditText;
import vn.game.clsWordDetail;
import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnKeyListener;
import android.widget.BaseAdapter;
import android.widget.GridView;

public class EditViewAdapter extends BaseAdapter {
	Context myContext;
	Activity activity;
	private String tag = EditViewAdapter.class.getSimpleName();
	private int count;
	private int countID=0;
	private int wText = 35;
	public int idEdit=0;
	private int row =1;
	private int column = 1;
	private ArrayList<clsWordDetail>  arrList;

//	public EditViewAdapter(Context _MyContext, int count) {
//		myContext = _MyContext;
//		this.count = count;
//	}
	
	public EditViewAdapter(Activity activity, ArrayList<clsWordDetail> arrList, int count) {
//		this.myContext = myContext;
		this.count = count;
		this.arrList = arrList;
		this.activity = activity;
	}
	
	public EditViewAdapter(Activity activity,  int count) {
//		this.myContext = myContext;
		this.count = count;
//		this.arrList = arrList;
		this.activity = activity;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public int getCount() {
		return count;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ExtEditText edt = null;

		try {
			if (convertView == null) {
				countID++;
				// Log.i(tag,"EditViewAdapter() count:" + countID);
				// Create EditText
//				edt = new ExtEditText(myContext);
				edt = new ExtEditText(activity);
				edt.setId(countID);
				edt.setWidth(wText);
				
				// edt.setText(countID + "");

				// Set layout_width and layout height
				edt.setLayoutParams(new GridView.LayoutParams(30, 30));
				// Hide EditTesxt
				edt.setVisibility(View.INVISIBLE);

				// Set length of EditText
				InputFilter[] FilterArray = new InputFilter[1];
				FilterArray[0] = new InputFilter.LengthFilter(1);
				edt.setFilters(FilterArray);

				edt.setOnKeyListener(new OnKeyListener() {

					public boolean onKey(View v, int keyCode, KeyEvent event) {
						// TODO Auto-generated method stub
						ExtEditText edtT = (ExtEditText) v;
//						setColorItemList(edtT.getnumEdt(), edtT.isAcross());
						return false;
					}
				});

				edt.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						// TODO Auto-generated method stub
						ExtEditText edtT = (ExtEditText) v;
//						setColorItemList(edtT.getnumEdt(), edtT.isAcross());
					}
				});

				edt.addTextChangedListener(new TextWatcher() {
					public void onTextChanged(CharSequence s, int start, int before, int count) {
						// TODO Auto-generated method stub

					}

					public void beforeTextChanged(CharSequence s, int start, int count, int after) {
						// TODO Auto-generated method stub

					}

					public void afterTextChanged(Editable str) {
						try {
							if (str.toString().trim().equals(""))
								return;

//							ExtEditText edt = (ExtEditText) findViewById(idEdit);
//							if (edt == null)
//								return;
//							// set caps lock
//							char[] cc = str.toString().trim().toCharArray();
//							if (Character.isLowerCase(cc[0])) {
//								edt.setText(str.toString().toUpperCase());
//							}
						} catch (Exception ex) {
							Log.e(tag + "afterTextChanged", "Error: " + ex.getMessage());
						}
					}
				});

				edt.setOnFocusChangeListener(new OnFocusChangeListener() {
					public void onFocusChange(View v, boolean hasFocus) {
						if (hasFocus) {
							idEdit = v.getId();
						}
					}
				});

			} else {
				// Log.i(tag,"getView().... ");
				edt = (ExtEditText) convertView;
			}

			if (countID == (row * column) + 1) {
				// Load data to GridView
//				loadGird(false);
				// Set width
//				int pageWidth = hScroll.getWidth();
//				hScroll.smoothScrollTo(pageWidth, 0);
			}
		} catch (Exception ex) {
			Log.e(tag, "getView() Error: " + ex.getMessage());
		}
		// return MyView;
		return edt;
	}

	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}
	
//	private void loadGird(boolean text) {
//		int id;
//		int i = 0, j = 0;
//		Log.i(tag, "loadGrid() text:" + text);
//		try {
//			// clearGrid();
//			for (clsWordDetail wd : arrList) {
//				char[] chWords = wd.getWords().toCharArray();
//				if (wd.isAcross()) {
//					for (i = 0; i < chWords.length; i++) {
//						ExtEditText edt = (ExtEditText) activity.findViewById(wd.getPos() + i);
//						edt.setAcross(wd.isAcross());
//						edt.setNumEdt(wd.getNum());
//						edt.setVisibility(View.VISIBLE);
//						if (text)
//							edt.setText(chWords[i] + "");
//						if (i == 0) {
//							edt.setPos(wd.getPos());
//						}
//					}
//
//				} else {
//					for (j = 0; j < chWords.length; j++) {
//						id = wd.getPos() + column * j;
//						if (id < row * column) {
//							ExtEditText edt = (ExtEditText) activity.findViewById(id);
//							edt.setAcross(wd.isAcross());
//							edt.setNumEdt(wd.getNum());
//							edt.setVisibility(View.VISIBLE);
//							if (text)
//								edt.setText(chWords[j] + "");
//							if (j == 0) {
//								edt.setPos(wd.getPos());
//							}
//						}
//					}
//				}
//			}
//		} catch (Exception ex) {
//			Log.e(tag, "loadGird() i,j=" + i + "," + j + "; Error: " + ex.getMessage());
//		}
//
//	}

//	private void setColorItemList(int posCell, boolean isAcross) {
//		try {
//			if (isAcross == true) {
//				for (int i = 0; i < acrossHint.size(); i++) {
//					if (acrossHint.get(i) == posCell) {
//						acrossAdapter.setSelectedPosition(i);
//						downAdapter.setSelectedPosition(-1);
////						lstAcross.setSelectionFromTop(i, 0);
//					}
//				}
//			} else {
//				for (int i = 0; i < downHint.size(); i++) {
//					if (downHint.get(i) == posCell) {
//						downAdapter.setSelectedPosition(i);
//						acrossAdapter.setSelectedPosition(-1);
////						lstDown.setSelectionFromTop(i, 0);
//					}
//				}
//			}
//		} catch (Exception ex) {
//			Log.e(tag, "setColorItemList() Error:" + ex.getMessage());
//		}
//	}
}